package dao.impl;

import dao.UserDao;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import pojo.User;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {
    @Override
    public List<User> findAll() {
       String resource = "mybatis-config.xml";
        SqlSession sqlSession = null;
        try {
           InputStream resourceAsStream = Resources.getResourceAsStream(resource);
//创建会话工厂对象
           SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
           sqlSession = sqlSessionFactory.openSession();
           List<User> users= sqlSession.getMapper(UserDao.class).findAll();
           return users;
       }catch (IOException e){
           throw new RuntimeException(e);
       }finally {
           sqlSession.close();
       }

    }
}
